﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using BearUtil;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
using FileMode = System.IO.FileMode;

public class CreateNewNode : CreateNodeBasic<CreateNewNode>
{
    public const string MenuName = "创建新节点";

    public override string DefaultPath
    {
        get { return "Assets/StoryEditor/CreateNewNode/CreateNewNode.asset"; }
    }

    public override string GeneratePath
    {
        get => "Assets/StoryEditor/Nodes/Action/";
    }

    public override string FormatPath
    {
        get => "Assets/StoryEditor/Nodes/";
    }

    public override string FileName
    {
        get => "DialogData.cs";
    }

    [Button("创建新节点",ButtonSizes.Large), TabGroup("使用")]
    public override void NewNode()
    {
        CtrlAllDynamicNode();

        if (string.IsNullOrEmpty(NodeName) || _nodes.Find(n => n.Name == NodeName))
        {
            Debug.LogError("节点名称不能为空 || 节点重名，重新定义");
            return;
        }

        if (node == null)
        {
            node = ScriptableObject.CreateInstance<DynamicNode>();
        }

        var nodePath = GeneratePath + NodeName + ".asset";
        node.Name = NodeName;
        AssetDatabase.CreateAsset(node, nodePath);
        AssetDatabase.Refresh();

        CtrlAllDynamicNode();
    }

    [Button("刷新数据集合", ButtonSizes.Large), GUIColor(0.2f, 0.8f, 0.6f)]
    public void UpdateFormat()
    {
        List<NodeProperty> props = new List<NodeProperty>();
        for (int i = 0; i < _nodes.Count; i++)
        {
            var node = _nodes[i];
            foreach (var nodeProperty in node.props)
            {
                if (props.Find(p => p.PropName == nodeProperty.PropName) == null)
                    props.Add(nodeProperty);
            }
        }

        var filePath = NodeMethodUtil.Delete(FormatPath, FileName);
        using (var sw = new StreamWriter(File.Open(filePath, FileMode.OpenOrCreate), Encoding.UTF8))
        {
            var scriptTxt = new StringBuilder();
            scriptTxt.Append("using System.ComponentModel;\n");
            scriptTxt.Append("public class DialogData\n");
            scriptTxt.Append("{\n");
            scriptTxt.Append("\t[Description(\"顺序\")] public int id;\n");
            scriptTxt.Append("\t[Description(\"类型\")] public int type;\n");
            scriptTxt.Append("\t[Description(\"是否属于存档点\")] public int back_step;\n");
            scriptTxt.Append("\t[Description(\"子节点\")] public int[][] child_node;\n");

            // 增加静态不可修改属性
            for (int i = 0; i < StaticProperty.Count; i++)
            {
                var p = StaticProperty[i];
                scriptTxt.Append(GetPropLine(p));
            }

            for (int i = 0; i < props.Count; i++)
            {
                var p = props[i];

                if (p.PropName.Equals("id") ||
                    p.PropName.Equals("type") ||
                    p.PropName.Equals("back_step") ||
                    p.PropName.Equals("child_node") ||
                    StaticProperty.Find(prop => prop.PropName.Equals(p.PropName)) != null)
                    continue;
                scriptTxt.Append(GetPropLine(p));
            }

            scriptTxt.Append("}\n");

            sw.Write(scriptTxt.ToString());
            sw.Close();
        }

        Debug.Log("【CreateNewNode】 Format script 生成成功");
    }
}